<?php
function connect($query)
    {
    $conn = mysql_connect('localhost', 'root', '') or die("Couldn't connect to database");
    mysql_select_db('bepgas_db', $conn) or die("Database doesn't exists");
    mysql_set_charset("utf8", $conn);
    $result = mysql_query($query, $conn);
    mysql_close();
    return $result;
}

function getNewProducts() {
    $query = "select * from products order by ProductID desc limit 3";
    $result = connect($query);
    return $result;
}

function getProductDetail() {
    if (isset($_REQUEST['id'])) {
        $id = $_REQUEST['id'];
        $query = "select * from tbl_products inner join tbl_images on tbl_products.pro_id=tbl_images.pro_id where tbl_products.pro_id = $id";
        $result = connect($query);
        return $result;
    } else {
        echo 'Dont be stupid';
    }
}

//load san pham ra trang index 
// bep gas am
function getProduct_Bepam(){
    $query="select name,price,url,tbl_products.pro_id from tbl_products inner join tbl_images on tbl_products.pro_id=tbl_images.pro_id where tbl_products.cat_id=0";
    $result=connect($query);
    return $result;
}
// bep gas duong
function getProduct_Bepduong(){
    $query="select name,price,url,tbl_products.pro_id from tbl_products inner join tbl_images on tbl_products.pro_id=tbl_images.pro_id where tbl_products.cat_id=1";
    $result=  connect($query);
    return $result;
}

// danh muc san pham
function getCategorie(){
    $query="select * from tbl_categories";
    $result= connect($query);
    return $result;
}

//Get current URL
function getCurrentUrl() {
    $url = isset($_SERVER['HTTPS']) ? 'https://' : 'http://';
    $url = $url . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    return $url;
}

//Get user IP
function getUserIP()
{
    $client  = @$_SERVER['HTTP_CLIENT_IP'];
    $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
    $remote  = $_SERVER['REMOTE_ADDR'];

    if(filter_var($client, FILTER_VALIDATE_IP))
    {
        $ip = $client;
    }
    elseif(filter_var($forward, FILTER_VALIDATE_IP))
    {
        $ip = $forward;
    }
    else
    {
        $ip = $remote;
    }

    return $ip;
}
// Tim kiem
